package com.newenergy.wisehome.update;


import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.newenergy.wisehome.MainActivity;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.MyEvent;
import com.newenergy.wisehome.common.helper.SPUtil;
import com.newenergy.wisehome.common.helper.ToastUtil;
import com.newenergy.wisehome.config.Constants;
import com.newenergy.wisehome.data.bean.UpdateBean;
import com.newenergy.wisehome.ui.widget.CustomDialog;

import org.greenrobot.eventbus.EventBus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;

import static com.newenergy.wisehome.config.Constants.ISDOWNLOAD;

/**
 * Created by Crazyfzw on 2016/8/21.
 * CheckVersionInfoTask.java
 * 从服务器取得版本信息，与本地apk对比版本号，判断是否有更新，然后用Dialog让用户选择是否更新
 * 若用户选择更新，则调用服务去完成apk文件的下载
 */
public class CheckVersionInfoTask extends AsyncTask<Void, Void, String> {

    private static final String TAG = "CheckVersionInfoTask";
    private ProgressDialog dialog;
    @SuppressLint("StaticFieldLeak")
    private Context mContext;
    private boolean mShowProgressDialog;
    private static final String VERSION_INFO_URL = "http://39.108.228.154:81/Jsons/BallLight.json";

    public CheckVersionInfoTask(Context context, boolean showProgressDialog) {
        mContext = context;
        mShowProgressDialog = showProgressDialog;

    }

    //初始化显示Dialog
    protected void onPreExecute() {
        if (isValidContext(mContext)&&mShowProgressDialog) {
            dialog = new ProgressDialog(mContext);
            dialog.setMessage(mContext.getString(R.string.check_new_version));
            dialog.show();
        }
    }

    //在后台任务(子线程)中检查服务器的版本信息
    @Override
    protected String doInBackground(Void... params) {
        return getVersionInfo(VERSION_INFO_URL);
    }


    //后台任务执行完毕后，解除Dialog并且解析return返回的结果
    @Override
    protected void onPostExecute(String result) {

        if (isValidContext(mContext)&&dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        if (!TextUtils.isEmpty(result)) {
            parseJson(result);
        }
    }


    /**
     * {
     "id":"1",
     "name":"球泡灯",
     "versionName":"3.00.01",
     "versionCode":1,
     "updateInfo":"更新协议",
     "updateTime":"2017-12-27 12:01:01",
     "apkDir":"http://39.108.228.154:81/APKS/BallLight/BallLight-hz.apk"
     }
     * @return
     */
    public String getVersionInfo(String urlStr){
        HttpURLConnection uRLConnection = null;
        InputStream is = null;
        BufferedReader buffer = null;
        String result = null;
        try {
            URL url = new URL(urlStr);
            uRLConnection = (HttpURLConnection) url.openConnection();
            uRLConnection.setRequestMethod("GET");
            is = uRLConnection.getInputStream();
            buffer = new BufferedReader(new InputStreamReader(is));
            StringBuilder strBuilder = new StringBuilder();
            String line;
            while ((line = buffer.readLine()) != null) {
                strBuilder.append(line);
            }
            result = strBuilder.toString();
        } catch (Exception e) {
            Log.e(TAG, "http post error");
        } finally {
            if (buffer != null) {
                try {
                    buffer.close();
                } catch (IOException ignored) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignored) {

                }
            }
            if (uRLConnection != null) {
                uRLConnection.disconnect();
            }
        }
        return result;
    }

    /**
     *
     * @param result
     */
    private void parseJson(String result) {
        Type listType = new TypeToken<LinkedList<UpdateBean>>(){}.getType();
        Gson gson = new Gson();
        LinkedList<UpdateBean> updateBeans = gson.fromJson(result, listType);
        for (Iterator iterator = updateBeans.iterator(); iterator.hasNext();) {
            UpdateBean updateBean = (UpdateBean) iterator.next();
            if (updateBean.getId().equals("11")){
                String apkUrl = updateBean.getApkDir();          //APK下载路径
                String updateMessage =updateBean.getUpdateInfo();//版本更新说明
                int apkCode =updateBean.getVersionCode();        //新版APK对于的版本号

                //取得已经安装在手机的APP的版本号 versionCode
                int versionCode = getCurrentVersionCode();

                //对比版本号判断是否需要更新
                if (apkCode > versionCode) {

                    showDialog(updateMessage, apkUrl);

                } else if (mShowProgressDialog) {
//                    Toast.makeText(mContext, mContext.getString(R.string.there_no_new_version), Toast.LENGTH_SHORT).show();
                    EventBus.getDefault().post(new MyEvent(Constants.NONEWVERSION,mContext.getString(R.string.there_no_new_version)));
                }
            }

        }

    }

    /**
     * 取得当前版本号
     * @return
     */
    public int getCurrentVersionCode() {

            try {
                return mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
            } catch (PackageManager.NameNotFoundException ignored) {
            }
        return 0;
    }


    /**
     * 显示对话框提示用户有新版本，并且让用户选择是否更新版本
     * @param content
     * @param downloadUrl
     */
  /*  public void showDialog(String content, final String downloadUrl) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle(R.string.dialog_choose_update_title);
            builder.setMessage(Html.fromHtml(content))
                    .setPositiveButton(R.string.dialog_btn_confirm_download, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //下载apk文件
                            goToDownloadApk(downloadUrl);
                        }
                    })
                    .setNegativeButton(R.string.dialog_btn_cancel_download, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        }
                    });

            AlertDialog dialog = builder.create();
            //点击对话框外面,对话框不消失
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
    }*/


    public void showDialog(String content, final String downloadUrl){
        CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
        builder.setMessageSize(20);
        builder.setTitle(R.string.dialog_choose_update_title);
        builder.setMessage(Html.fromHtml(content));
        builder.setPositiveButton(R.string.dialog_btn_confirm_download, (dialog1, which) -> {
            //下载apk文件
            goToDownloadApk(downloadUrl);
            ToastUtil.toast(mContext.getString(R.string.bg_update));//程序进入后台更新中
            SPUtil.put(ISDOWNLOAD,true);
            dialog1.dismiss();
        });

        builder.setNegativeButton(R.string.dialog_btn_cancel_download,
                (dialog12, which) -> dialog12.dismiss());

        CustomDialog customDialog = builder.create();
        customDialog.setCanceledOnTouchOutside(false);
        customDialog.show();
    }


    /**
     * 用intent启用DownloadService服务去下载AKP文件
     * @param downloadUrl
     */
    private void goToDownloadApk(String downloadUrl) {
        Intent intent = new Intent(mContext, DownloadApkService.class);
        intent.putExtra("apkUrl", downloadUrl);
        mContext.startService(intent);
    }


    private boolean isValidContext (Context c){

        MainActivity a = (MainActivity)c;

        if (a.isDestroyed() || a.isFinishing()){
            return false;
        }else{
            return true;
        }
    }


}
